<?php
	header("content-type:text/html;charset=utf-8");
	require_once 'lib/smarty/smarty.class.php';	
	session_start();
	$smarty=new Smarty();
	$mysqli = mysqli_connect("localhost","root","","aaa");
	if($_POST){
		$email=$_POST['email'];
		$pwd=md5($_POST['pwd']);
		$sel_sql="select * from user where email='".$email."'";
		$result = $mysqli->query($sel_sql);
		//$field_arr = $result->fetch_fields();
		$data=array();
		while($row = $result->fetch_assoc())
		{
			$data[] = $row;
		}
		if(!$data){
			//未注册
			echo 1;exit;
		}else{
			
			if($data[0]['pwd']==$pwd){
				//登陆成功
				$_SESSION['aa_user']=$data[0];
				setcookie("aa_email",$data[0]['email'],time()+2*7*24*3600);
				setcookie("aa_pwd",$data[0]['pwd'],time()+2*7*24*3600);
				
				echo 2;exit;
			}else{
				//密码错误
				echo 3;exit;
			}
		}
		
	}else{
		if($_COOKIE['aa_email'] && $_COOKIE['aa_pwd']){
			$sel_sql="select * from user where email='".$_COOKIE['aa_email']."' and pwd='".$_COOKIE['aa_pwd']."'";
			$result = $mysqli->query($sel_sql);
			$data=array();
			while($row = $result->fetch_assoc())
			{
				$data[] = $row;
			}
			if(!$data){
				$smarty->display('login.html');exit;
			}else{
				$_SESSION['aa_user']=$data[0];
				header("Location: file.php"); 
			}
		}else{
			$smarty->display('login.html');
		}
		
	}